package com.shopx.config.controller;


import com.shopx.base.anno.RepearSubmitChkAnno;
import com.shopx.base.anno.ShopxParamAnno;
import com.shopx.base.dto.Result;
import com.shopx.base.utils.ResultUtil;
import com.shopx.cmm.DTO.PageDTO;
import com.shopx.config.bean.SysDict;
import com.shopx.config.model.SysDictModel;
import com.shopx.config.service.ISysDictService;
import com.shopx.core.DTO.ExcelObjectDTO;
import com.shopx.core.common.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lituo
 * @className: SysDictController
 * @description: TODO
 * @date 2021/12/5 12:04 PM
 */
@RestController
@Slf4j
@RequestMapping(value = "/sysDict")
public class SysDictController {
    @Autowired
    private ISysDictService sysDictService;

    /**
     * 查询分页记录
     *
     * @param SysDict
     * @return
     * @author lituo
     * @date 11:42 AM 2021/12/5
     */
    @PostMapping("/queryPageList")
    public Result<SysDictModel> queryPageList(@RequestBody SysDictModel SysDict) {
        PageDTO<SysDictModel> pageDTO = sysDictService.getListPage(SysDict);
        return ResultUtil.ok(pageDTO);
    }

    /**
     * 新增记录
     *
     * @param SysDict
     * @return
     * @author lituo
     * @date 11:42 AM 2021/12/5
     */
    @PostMapping("/insertOne")
    @RepearSubmitChkAnno(lockTime = CommonConstant.REQ_TM)
    @ShopxParamAnno
    public Result<SysDict> insertOne(@RequestBody SysDict SysDict) {
        Integer result = sysDictService.insertSelective(SysDict);
        return ResultUtil.ok(result);
    }

    /**
     * 修改记录
     *
     * @param SysDict
     * @return
     * @author lituo
     * @date 11:42 AM 2021/12/5
     */
    @PostMapping("/updateOne")
    @RepearSubmitChkAnno(lockTime = CommonConstant.REQ_TM)
    @ShopxParamAnno
    public Result<?> updateOne(@RequestBody SysDict SysDict) {
        Integer result = sysDictService.updateByPrimaryKeySelective(SysDict);
        return ResultUtil.ok(result);
    }

    /**
     * 查询明细记录
     *
     * @param SysDict
     * @return
     * @author lituo
     * @date 11:42 AM 2021/12/5
     */
    @PostMapping(value = "/queryOneByPrimaryKey")
    public Result<SysDictModel> queryOneByPrimaryKey(@RequestBody SysDict SysDict) {
        SysDictModel result = sysDictService.selectByPrimaryKey(SysDict);
        return ResultUtil.ok(result);
    }

    /**
     * 删除单条记录
     *
     * @param SysDict
     * @return
     * @author lituo
     * @date 11:42 AM 2021/12/5
     */
    @PostMapping(value = "/deleteChecked")
    @RepearSubmitChkAnno(lockTime = CommonConstant.REQ_TM)
    public Result<SysDictModel> deleteChecked(@RequestBody SysDict SysDict) {
        int result = sysDictService.deleteByPrimaryKey(SysDict);
        return ResultUtil.ok(result);
    }

    @PostMapping(value = "/deleteBatch")
    @RepearSubmitChkAnno(lockTime = CommonConstant.REQ_TM)
    public Result<?> deleteBatch(@RequestBody List<SysDict> list) {
        sysDictService.deleteBatch(list);
        return ResultUtil.ok(true);
    }


    @PostMapping(value = "/queryAllList")
    @ShopxParamAnno
    public Result<?> queryAllList(@RequestBody SysDictModel sysDict) {
        List<SysDictModel> list = sysDictService.selectList(sysDict);
        return ResultUtil.ok(list);
    }

    @PostMapping(value = "/queryValueByName")
    public Result<String> queryValueByName(@RequestBody SysDict sysDict) {
        String s = sysDictService.queryValueByName(sysDict.getDictCode(), sysDict.getDictName());
        return ResultUtil.ok(s);
    }

    @RequestMapping(value = "/download")
    public void download(SysDictModel sysDict, HttpServletResponse response) {
        ExcelObjectDTO dto = new ExcelObjectDTO();
        dto.setSubTitle("字典导出清单");
        dto.setTitle(new String[]{"dictId:编号", "dictCode:枚举", "dictName:编码", "dictValue:值", "dictDesc:描述", "dictSort:排序"});
        PageDTO<SysDictModel> pageDTO = sysDictService.getListPage(sysDict);
        dto.setObj(pageDTO.getData());
        dto.setSize(pageDTO.getiTotalDisplayRecords());
        ResultUtil.excelDownload(dto, response);
    }

}
